﻿using LitJson;
using Native.Tool.Http;
using System.Collections.Generic;
using System.Text;

namespace github.heerheer.card2.UI.Infos
{
    public class NewsInfo
    {
        public int id { get; set; }
        public string Title { get; set; }
        public string Message { get; set; }
        public string User { get; set; }

    }
    public class NewsHelper
    {
        public static List<NewsInfo> getNews()
        {
            List<NewsInfo> infos = new List<NewsInfo>();
            var url = "http://customcard.heerdev.top/news.php?special=1";
            var returnstr = Encoding.UTF8.GetString(HttpWebClient.Get(url));
            var json = LitJson.JsonMapper.ToObject(returnstr);
            if (json["err_code"].ToString() != "0")
            {
                infos.Add(new NewsInfo() { Message = "错了...", User = "系统", Title = "那里不可以..." });
                return infos;
            }
            var jsonData = json["data"];

            foreach (JsonData item in jsonData)
            {
                infos.Add(new NewsInfo() { Message = item["NewsText"].ToString(), User = item["User"].ToString(), Title = item["Name"].ToString() });

            }
            return infos;
        }
        public static List<NewsInfo> getShares()
        {
            List<NewsInfo> infos = new List<NewsInfo>();
            var url = "http://hd215.api.yesapi.cn/?&s=App.Table.FreeQuery&model_name=card2news&logic=and&where=[[\"infotype\", \"=\", \"share\"]]&order=[\"add_time desc\"]&app_key=33CE8DB4D3331D7B5B1BBBD3326F8CD1";
            var returnstr = Encoding.UTF8.GetString(HttpWebClient.Get(url));
            var json = LitJson.JsonMapper.ToObject(returnstr);
            json = json["data"];
            if (json["err_code"].ToString() != "0")
            {
                infos.Add(new NewsInfo() { Message = "错了...", User = "系统", Title = "那里不可以..." });
                return infos;
            }

            foreach (JsonData item in json["list"])
            {
                infos.Add(new NewsInfo() { Message = item["news"].ToString(), User = item["nuser"].ToString(), Title = item["title"].ToString() });

            }
            return infos;
        }
    }
}
